---
title: "Replication Materials" 
subtitle: "Context Matters: Understanding Student Usage, Skills, and Attitudes Toward AI to Inform Classroom Policies"
author: "Cahill and McCabe"
output:
  html_document:
    toc: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Loading Data

The original analysis was run on R version 4.2.0 on macOS Catalina 10.15.6

```{r, warning=F, message=F}
library(tidyverse)
library(ggplot2)
library(scales)
library(gtsummary)
library(patchwork)
ai <- read_csv("aianalysis.csv")
```


# Figure 1: Proportion of Respondents Using ChatGPT for Different Purposes 


```{r }
ai_usage <- ai %>% 
  select(usage_entertainment, usage_worktasks,
         usage_language,
         usage_recipes,
         usage_newideas,
         usage_coursework,
         usage_writingpapers,
         usage_assignments,
         usage_data,
         usage_music,
         usage_travel, usage_creativewriting)%>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value)
ai_usage$types <- c("Entertainment", "Work Tasks",
                    "Learning a Language", "Recipes/Cooking",
                    "New ideas", "Sources for coursework",
                    "Writing papers", "Help with assignments",
                    "Analyzing data", "Creating music",
                    "Travel ideas", "Creative writing")

ggplot(ai_usage, aes(x = types, 
                     y = value))  +
  geom_segment(aes(x=types, xend=types, y=0, yend=value)) +
  geom_point(stat="identity", size=3.5) +
  labs(title = "Proportion at Least Rarely Use ChatGPT For...",
       x = "",
       y = "Proportion")+
  theme_minimal()+
  coord_flip()
ggsave("figure1.pdf", width=6, height=2.8, units="in")

```


# Figure 2 Demographic Differences in ChatGPT Usage 

```{r, fig.width=14, fig.height=8} 
## Creates four figures, displayed together in the paper

ai_usage <- ai %>% 
  select(usage_entertainment, usage_worktasks,
         usage_language,
         usage_recipes,
         usage_newideas,
         usage_coursework,
         usage_writingpapers,
         usage_assignments,
         usage_data,
         usage_music,
         usage_travel, usage_creativewriting, firstgen_binary)%>%
  group_by(firstgen_binary) %>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value,-firstgen_binary)


ai_usage$firstgen_binary <- as.factor(ai_usage$firstgen_binary)
ai_usage$types <- c(rep("Entertainment", 2), rep("Work Tasks", 2),
                    rep("Learning a Language", 2), rep("Recipes/Cooking", 2),
                    rep("New ideas", 2), rep("Sources for coursework", 2),
                    rep("Writing papers", 2), rep("Help with assignments", 2),
                    rep( "Analyzing data", 2), rep("Creating music", 2),
                    rep("Travel ideas", 2), rep("Creative writing", 2))

f21 <- ggplot(ai_usage, aes(x = types, 
                     y = value))  +
  geom_line(alpha=.6) +
  geom_point(stat="identity", size=3.5, aes(color=firstgen_binary), alpha=.75) +
  labs(title = "Proportion at Least Rarely Use ChatGPT For...",
       x = "",
       y = "Proportion")+
  theme_minimal()+
  theme(legend.position = "bottom")+
  scale_color_manual(name = "", labels = c("Parents Graduated College", "Parents Did Not Graduate College"), values=c("dodgerblue", "orange"))+
  coord_flip()
#ggsave("aiusage_firstgen.pdf", width=6, height=3.3, units="in")

 ai_usage <- ai %>% 
  select(usage_entertainment, usage_worktasks,
         usage_language,
         usage_recipes,
         usage_newideas,
         usage_coursework,
         usage_writingpapers,
         usage_assignments,
         usage_data,
         usage_music,
         usage_travel, usage_creativewriting, female)%>%
  filter(!is.na(female))%>%
  group_by(female) %>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value,-female)
ai_usage$female <- as.factor(ai_usage$female)
ai_usage$types <- c(rep("Entertainment", 2), rep("Work Tasks", 2),
                    rep("Learning a Language", 2), rep("Recipes/Cooking", 2),
                    rep("New ideas", 2), rep("Sources for coursework", 2),
                    rep("Writing papers", 2), rep("Help with assignments", 2),
                    rep( "Analyzing data", 2), rep("Creating music", 2),
                    rep("Travel ideas", 2), rep("Creative writing", 2))


f22 <-ggplot(ai_usage, aes(x = types, 
                     y = value))  +
  geom_line(alpha=.6) +
  geom_point(stat="identity", size=3.5, aes(color=female), alpha=.75) +
  labs(title = "Proportion at Least Rarely Use ChatGPT For...",
       x = "",
       y = "Proportion")+
  theme_minimal()+
  theme(legend.position = "bottom")+
  scale_color_manual(name = "Gender", values=c("dodgerblue", "orange"))+
  coord_flip()
#ggsave("aiusage_gender.pdf", width=6, height=3.3, units="in")


## gpa

ai_usage <- ai %>% 
  select(usage_entertainment, usage_worktasks,
         usage_language,
         usage_recipes,
         usage_newideas,
         usage_coursework,
         usage_writingpapers,
         usage_assignments,
         usage_data,
         usage_music,
         usage_travel, usage_creativewriting, highgpa)%>%
  filter(!is.na(highgpa))%>%
  group_by(highgpa) %>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value,-highgpa)
ai_usage$highgpa <- as.factor(ai_usage$highgpa)
ai_usage$types <- c(rep("Entertainment", 2), rep("Work Tasks", 2),
                    rep("Learning a Language", 2), rep("Recipes/Cooking", 2),
                    rep("New ideas", 2), rep("Sources for coursework", 2),
                    rep("Writing papers", 2), rep("Help with assignments", 2),
                    rep( "Analyzing data", 2), rep("Creating music", 2),
                    rep("Travel ideas", 2), rep("Creative writing", 2))


f23 <- ggplot(ai_usage, aes(x = types, 
                     y = value))  +
  geom_line(alpha=.6) +
  geom_point(stat="identity", size=3.5, aes(color=highgpa), alpha=.75) +
  labs(title = "Proportion at Least Rarely Use ChatGPT For...",
       x = "",
       y = "Proportion")+
  theme_minimal()+
  theme(legend.position = "bottom")+
  scale_color_manual(name = "", values=c("dodgerblue", "orange"))+
  coord_flip()
#ggsave("aiusage_gpa.pdf", width=6, height=3.3, units="in")




 ai_usage <- ai %>% 
  select(usage_entertainment, usage_worktasks,
         usage_language,
         usage_recipes,
         usage_newideas,
         usage_coursework,
         usage_writingpapers,
         usage_assignments,
         usage_data,
         usage_music,
         usage_travel, usage_creativewriting, racegroupsmall)%>%
  filter(!is.na(racegroupsmall))%>%
  group_by(racegroupsmall) %>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value,-racegroupsmall)
ai_usage$female <- as.factor(ai_usage$racegroupsmall)
ai_usage$types <- c(rep("Entertainment", 3), rep("Work Tasks", 3),
                    rep("Learning a Language", 3), rep("Recipes/Cooking", 3),
                    rep("New ideas", 3), rep("Sources for coursework", 3),
                    rep("Writing papers", 3), rep("Help with assignments", 3),
                    rep( "Analyzing data", 3), rep("Creating music", 3),
                    rep("Travel ideas", 3), rep("Creative writing", 3))


f24 <-ggplot(ai_usage, aes(x = types, 
                     y = value))  +
  geom_line(alpha=.6) +
  geom_point(stat="identity", size=3.5, aes(color=racegroupsmall), alpha=.65) +
  labs(title = "Proportion at Least Rarely Use ChatGPT For...",
       x = "",
       y = "Proportion")+
  theme_minimal()+
  theme(legend.position = "bottom")+
  scale_color_manual(name = "Race/Ethnicity", values=c("dodgerblue", "orange", "green3"))+
  coord_flip()
#ggsave("aiusage_rg.pdf", width=6, height=3.3, units="in")


f21 + f22 + f23 + f24 +
  plot_layout(ncol=2 )
ggsave("figure2.pdf", width=12, height=7, units="in")
```


# Figure 3: AI Skills Self-Assessment 

```{r}

ai_skills_long <- ai %>% 
  select(skills_competent3, skills_integritypol3,
         skills_improvewriting3, skills_data3,
         skills_notviolate3, skills_helpsexams3)%>%
  gather(var, val)


sktypes <- c("skills_competent3"="Competent with AI", 
             "skills_integritypol3"="Know how to adhere \n to integrity policy",
         "skills_improvewriting3"="Know how to use AI \n to improve writing", 
         "skills_data3"="Know how to use AI \n to visualize/analyze data",
         "skills_notviolate3"="Know how to not \n plagiarize with AI", 
         "skills_helpsexams3"="Know how to use AI \n to prepare for quizzes/exams")

ggplot(ai_skills_long, aes(x=as.factor(val), group=var,y = after_stat(prop)))+
  facet_wrap(~var, labeller = labeller(var = sktypes))+
  geom_bar(stat="count")+
  theme_bw()+
  theme(strip.background=element_rect(fill="white", color="white"),
        panel.border = element_blank())+
  coord_flip()+
  labs(y="Proportion of Respondents", x="", title="Proportion of R Agreeing/Disagreeing Have AI Skill")
ggsave("figure3.pdf", width=7.2, height=3, units="in")
```

# Figure 4: Perception of AI Appropriateness for Different Academic Tasks 

```{r}
aptypes <- c("app_writeessay3"="Writing essay components", 
             "app_researchpaper3"="Writing research \n paper components",
             "app_reliablesources3"="Finding reliable sources", 
             "app_studying3"="Studying for exams",
              "app_researchquestions3"="Generate research questions", 
             "app_math3"="Solving math problems",
                "app_dataviz3"=  "Data visualization", 
             "app_writingfeedback3"="Feedback on writing")


ai_approp_long <- ai %>% 
  select(app_writeessay3, app_researchpaper3,
         app_reliablesources3, app_studying3,
         app_researchquestions3, app_math3,
         app_dataviz3, app_writingfeedback3)%>%
  gather(var, val)


ggplot(ai_approp_long, aes(x=as.factor(val), group=var,y = after_stat(prop)))+
  facet_wrap(~var, labeller = labeller(var = aptypes))+
  geom_bar(stat="count")+
  theme_bw()+
  theme(strip.background=element_rect(fill="white", color="white"),
        panel.border = element_blank())+
  coord_flip()+
  labs(y="Proportion of Respondents", x="", title="Proportion Viewing AI Tasks as Appropriate or Inappropriate")
ggsave("figure4.pdf", width=7.2, height=4, units="in")
```


# Online Appendix

## Figure A1: Proportion of Respondents Using Different Academic Resources

```{r}
ai_tools <- ai %>% 
  select(use_chatgpt, use_spellcheck, use_grammar,
        use_translate, use_officehours, use_studygroups,
        use_learningcenter,
        use_tutoring, use_writingcenter, use_onlinecomputers)%>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value)
ai_tools$types <- c("ChatGPT", "Spell Check",
                    "Grammar Check", "Translation", "Office Hours",
                    "Study Groups", "Learning Centers",
                    "Tutoring", "Writing Center",
                    "Online Computers")


ggplot(ai_tools, aes(x = types, 
               y = value)) +
  geom_segment(aes(x=types, xend=types, y=0, yend=value)) +
  geom_point(stat="identity", size=3.5) +
  labs(title = "Proportion Already Use Tools in Academic Work",
       x = "",
       y = "Proportion")+
  theme_minimal()+
  coord_flip()
ggsave("figurea1.pdf", width=6, height=2.8, units="in")

```


## Figure A2: Perception of AI Tasks Inside and Outside of Classroom

```{r}
ethics <- ai %>% select(ethics_1, ethics_2,
              ethics_3, ethics_4, ethics_5, ethics_6,
              ethics_7, ethics_8) %>%
  gather(var, value)
ethics$Purpose <- NA
ethics$Purpose[ethics$var == "ethics_1"] <- "Military drones"
ethics$Purpose[ethics$var == "ethics_2"] <- "Risky jobs"
ethics$Purpose[ethics$var == "ethics_3"] <- "Teaching assistant"
ethics$Purpose[ethics$var == "ethics_4"] <- "Self-driven \n delivery trucks"
ethics$Purpose[ethics$var == "ethics_5"] <- "Facial recognition \n in public spaces"
ethics$Purpose[ethics$var == "ethics_6"] <- "Facial recognition \n for class attendance"
ethics$Purpose[ethics$var == "ethics_7"] <- "Write letters \n of recommendation"
ethics$Purpose[ethics$var == "ethics_8"] <- "Write papers and \n complete assignments"

ggplot(ethics, aes(x=value, group=Purpose,y = after_stat(prop)))+
  facet_wrap(~Purpose)+
  geom_bar(stat="count", fill="black")+
  theme_minimal()+
  coord_flip()+
  labs(y="Proportion", x="", title="Perception of Different AI Tasks")
ggsave("figurea2.pdf", width=6, height=4, units="in")

```


## Figure A3: Proportion who Somewhat or Strongly Agree that they have the Skill	

```{r, fig.width=14, fig.height=7}
ai_skills <- ai %>% 
  select(skills_competentAG, skills_integritypolAG,
         skills_improvewritingAG, skills_dataAG,
         skills_notviolateAG, skills_helpsexamsAG)%>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value)
ai_skills$types <- c("Competent with AI", "Know how to adhere to integrity policy",
                    "Know how to use AI to improve writing", "Know how to use AI to analyze data",
                    "Know how to not plagiarize with AI", "Know how to use AI to prepare for exams")


ggplot(ai_skills, aes(x = types, 
               y = value)) +
  geom_segment(aes(x=types, xend=types, y=0, yend=value)) +
  geom_point(stat="identity", size=4) +
  labs(title = "Proportion Agree that R has the Skill",
       x = "",
       y = "Proportion of Respondents")+
  theme_minimal()+
  coord_flip()
ggsave("figurea3.pdf", width=6, height=2.8, units="in", dpi=300)


ai_skills <- ai %>% 
  select(skills_competentAG, skills_integritypolAG,
         skills_improvewritingAG, skills_dataAG,
         skills_notviolateAG, skills_helpsexamsAG, firstgen_binary)%>%
  filter(!is.na(firstgen_binary))%>%
  group_by(firstgen_binary)%>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value, -firstgen_binary)
ai_skills$firstgen_binary <- as.factor(ai_skills$firstgen_binary)
ai_skills$types <- c(rep("Competent with AI", 2), rep("Know how to adhere to integrity policy", 2),
                    rep("Know how to use AI to improve writing", 2), rep("Know how to use AI to analyze data", 2),
                    rep("Know how to not plagiarize with AI", 2), rep("Know how to use AI to prepare for exams", 2))


a31 <- ggplot(ai_skills, aes(x = types, 
               y = value)) +
  geom_line(alpha=.65) +
  geom_point(stat="identity", size=3.5, aes(color=firstgen_binary), alpha=.75) +
  labs(title = "Proportion Agree that R has the Skill",
       x = "",
       y = "Proportion of Respondents")+
  theme_minimal()+
  theme(legend.position = "bottom")+
  scale_color_manual(name = "", labels = c("Parents Graduated College", "Parents Did Not Graduate College"), values=c("dodgerblue", "orange"))+
  coord_flip()
#ggsave("aiskillls_firstgenAG.png", width=6.8, height=3.3, units="in", dpi=300)



ai_skills <- ai %>% 
  select(skills_competentAG, skills_integritypolAG,
         skills_improvewritingAG, skills_dataAG,
         skills_notviolateAG, skills_helpsexamsAG, female)%>%
  filter(!is.na(female))%>%
  group_by(female)%>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value, -female)
ai_skills$female <- as.factor(ai_skills$female)
ai_skills$types <- c(rep("Competent with AI", 2), rep("Know how to adhere to integrity policy", 2),
                    rep("Know how to use AI to improve writing", 2), rep("Know how to use AI to analyze data", 2),
                    rep("Know how to not plagiarize with AI", 2), rep("Know how to use AI to prepare for exams", 2))


a32 <- ggplot(ai_skills, aes(x = types, 
               y = value)) +
  geom_line(alpha=.65) +
  geom_point(stat="identity", size=3.5, aes(color=female), alpha=.75) +
  labs(title = "Proportion Agree that R has the Skill",
       x = "",
       y = "Proportion of Respondents")+
  theme_minimal()+
  theme(legend.position = "bottom")+
  scale_color_manual(name = "Gender", values=c("dodgerblue", "orange"))+
  coord_flip()
#ggsave("aiskillls_genderAG.png", width=6.8, height=3.3, units="in", dpi=300)



ai_skills <- ai %>% 
  select(skills_competentAG, skills_integritypolAG,
         skills_improvewritingAG, skills_dataAG,
         skills_notviolateAG, skills_helpsexamsAG, highgpa)%>%
  filter(!is.na(highgpa))%>%
  group_by(highgpa)%>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value, -highgpa)
ai_skills$highgpa <- as.factor(ai_skills$highgpa)
ai_skills$types <- c(rep("Competent with AI", 2), rep("Know how to adhere to integrity policy", 2),
                    rep("Know how to use AI to improve writing", 2), rep("Know how to use AI to analyze data", 2),
                    rep("Know how to not plagiarize with AI", 2), rep("Know how to use AI to prepare for exams", 2))


a33 <- ggplot(ai_skills, aes(x = types, 
               y = value)) +
  geom_line(alpha=.65) +
  geom_point(stat="identity", size=3.5, aes(color=highgpa), alpha=.75) +
  labs(title = "Proportion Agree that R has the Skill",
       x = "",
       y = "Proportion of Respondents")+
  theme_minimal()+
  theme(legend.position = "bottom")+
  scale_color_manual(name = "GPA", values=c("dodgerblue", "orange"))+
  coord_flip()
#ggsave("aiskillls_gpaAG.png", width=6.8, height=3.3, units="in", dpi=300)




ai_skills <- ai %>% 
  select(skills_competentAG, skills_integritypolAG,
         skills_improvewritingAG, skills_dataAG,
         skills_notviolateAG, skills_helpsexamsAG, racegroupsmall)%>%
  filter(!is.na(racegroupsmall))%>%
  group_by(racegroupsmall)%>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value, -racegroupsmall)
ai_skills$racegroupsmall <- as.factor(ai_skills$racegroupsmall)
ai_skills$types <- c(rep("Competent with AI", 3), rep("Know how to adhere to integrity policy", 3),
                    rep("Know how to use AI to improve writing", 3), rep("Know how to use AI to analyze data", 3),
                    rep("Know how to not plagiarize with AI", 3), rep("Know how to use AI to prepare for exams", 3))


a34 <- ggplot(ai_skills, aes(x = types, 
               y = value)) +
  geom_line(alpha=.65) +
  geom_point(stat="identity", size=3.5, aes(color=racegroupsmall), alpha=.75) +
  labs(title = "Proportion Agree that R has the Skill",
       x = "",
       y = "Proportion of Respondents")+
  theme_minimal()+
  theme(legend.position = "bottom")+
  scale_color_manual(name = "Race/Ethnicity", values=c("dodgerblue", "orange", "green3"))+
  coord_flip()
#ggsave("aiskillls_rgAG.png", width=6.8, height=3.3, units="in", dpi=300)


a31 + a32 + a33 + a34 +
  plot_layout(ncol=2 )
ggsave("figurea3demo.pdf", width=14, height=7, units="in")
```



## Figure A4: Proportion of Respondents Perceiving AI Task as Somewhat/Extremely Appropriate


```{r, fig.width=14, fig.height=7}

ai_appropAG <- ai %>% 
  select(app_writeessayAG, app_researchpaperAG,
         app_reliablesourcesAG, app_studyingAG,
         app_researchquestionsAG, app_mathAG,
         app_datavizAG, app_writingfeedbackAG)%>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value)
ai_appropAG$types <- c("Writing essay components", "Writing research paper components",
                    "Finding reliable sources", "Studying for exams",
                    "Generate research questions", "Solving math problems",
                    "Data visualization", "Feedback on writing")


ggplot(ai_appropAG, aes(x = types, 
               y = value)) +
  geom_segment(aes(x=types, xend=types, y=0, yend=value)) +
  geom_point(stat="identity", size=4) +
  ylim(0, .8)+
  labs(title = "Proportion who View AI Task as Appropriate",
       x = "",
       y = "Proportion of Respondents")+
  theme_minimal()+
  coord_flip()
ggsave("figureA4.pdf", width=6, height=2.8, units="in", dpi=300)


ai_approp <- ai %>% 
  select(app_writeessayAG, app_researchpaperAG,
         app_reliablesourcesAG, app_studyingAG,
         app_researchquestionsAG, app_mathAG,
         app_datavizAG, app_writingfeedbackAG, firstgen_binary)%>%
  filter(!is.na(firstgen_binary)) %>%
  group_by(firstgen_binary) %>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value, -firstgen_binary)
ai_approp$firstgen_binary <- as.factor(ai_approp$firstgen_binary)
ai_approp$types <- c(rep("Writing essay components", 2), 
                     rep("Writing research paper components", 2),
                    rep("Finding reliable sources", 2), rep("Studying for exams", 2),
                    rep("Generate research questions", 2), rep("Solving math problems", 2),
                    rep("Data visualization", 2), rep("Feedback on writing", 2))


a41 <- ggplot(ai_approp, aes(x = types, 
               y = value)) +
  geom_line(alpha=.65) +
  geom_point(stat="identity", size=3.5, aes(color=firstgen_binary), alpha=.75) +
  ylim(0, 1)+
  labs(title = "Proportion Viewing AI Task as Appropriate",
       x = "",
       y = "Proportion of Respondents")+
  theme_minimal()+
    theme(legend.position = "bottom")+
  scale_color_manual(name = "", labels = c("Parents Graduated College", "Parents Did Not Graduate College"), values=c("dodgerblue", "orange"))+
  coord_flip()
#ggsave("aiapprop_firstgenAG.png", width=6.8, height=3.3, units="in", dpi=300)



ai_approp <- ai %>% 
  select(app_writeessayAG, app_researchpaperAG,
         app_reliablesourcesAG, app_studyingAG,
         app_researchquestionsAG, app_mathAG,
         app_datavizAG, app_writingfeedbackAG, female)%>%
  filter(!is.na(female)) %>%
  group_by(female) %>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value, -female)
ai_approp$female <- as.factor(ai_approp$female)
ai_approp$types <- c(rep("Writing essay components", 2), 
                     rep("Writing research paper components", 2),
                    rep("Finding reliable sources", 2), rep("Studying for exams", 2),
                    rep("Generate research questions", 2), rep("Solving math problems", 2),
                    rep("Data visualization", 2), rep("Feedback on writing", 2))


a42 <- ggplot(ai_approp, aes(x = types, 
               y = value)) +
  geom_line(alpha=.65) +
  geom_point(stat="identity", size=3.5, aes(color=female), alpha=.75) +
  ylim(0, 1)+
  labs(title = "Proportion Viewing AI Task as Appropriate",
       x = "",
       y = "Proportion of Respondents")+
  theme_minimal()+
    theme(legend.position = "bottom")+
  scale_color_manual(name = "Gender", values=c("dodgerblue", "orange"))+
  coord_flip()
#ggsave("aiapprop_genderAG.png", width=6.8, height=3.3, units="in", dpi=300)





ai_approp <- ai %>% 
  select(app_writeessayAG, app_researchpaperAG,
         app_reliablesourcesAG, app_studyingAG,
         app_researchquestionsAG, app_mathAG,
         app_datavizAG, app_writingfeedbackAG, highgpa)%>%
  filter(!is.na(highgpa)) %>%
  group_by(highgpa) %>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value, -highgpa)
ai_approp$highgpa <- as.factor(ai_approp$highgpa)
ai_approp$types <- c(rep("Writing essay components", 2), 
                     rep("Writing research paper components", 2),
                    rep("Finding reliable sources", 2), rep("Studying for exams", 2),
                    rep("Generate research questions", 2), rep("Solving math problems", 2),
                    rep("Data visualization", 2), rep("Feedback on writing", 2))


a43 <- ggplot(ai_approp, aes(x = types, 
               y = value)) +
  geom_line(alpha=.65) +
  geom_point(stat="identity", size=3.5, aes(color=highgpa), alpha=.75) +
  ylim(0, 1)+
  labs(title = "Proportion Viewing AI Task as Appropriate",
       x = "",
       y = "Proportion of Respondents")+
  theme_minimal()+
    theme(legend.position = "bottom")+
  scale_color_manual(name = "GPA", values=c("dodgerblue", "orange"))+
  coord_flip()
#ggsave("aiapprop_gpaAG.png", width=6.8, height=3.3, units="in", dpi=300)




ai_approp <- ai %>% 
  select(app_writeessayAG, app_researchpaperAG,
         app_reliablesourcesAG, app_studyingAG,
         app_researchquestionsAG, app_mathAG,
         app_datavizAG, app_writingfeedbackAG, racegroupsmall)%>%
  filter(!is.na(racegroupsmall)) %>%
  group_by(racegroupsmall) %>%
  summarise_all(., mean, na.rm=T) %>%
  gather(var, value, -racegroupsmall)
ai_approp$racegroupsmall <- as.factor(ai_approp$racegroupsmall)
ai_approp$types <- c(rep("Writing essay components", 3), 
                     rep("Writing research paper components", 3),
                    rep("Finding reliable sources", 3), rep("Studying for exams", 3),
                    rep("Generate research questions", 3), rep("Solving math problems", 3),
                    rep("Data visualization", 3), rep("Feedback on writing", 3))


a44 <- ggplot(ai_approp, aes(x = types, 
               y = value)) +
  geom_line(alpha=.65) +
  geom_point(stat="identity", size=3.5, aes(color=racegroupsmall), alpha=.75) +
  ylim(0, 1)+
  labs(title = "Proportion Viewing AI Task as Appropriate",
       x = "",
       y = "Proportion of Respondents")+
  theme_minimal()+
    theme(legend.position = "bottom")+
  scale_color_manual(name = "Race/Ethnicity", values=c("dodgerblue", "orange", "green3"))+
  coord_flip()
#ggsave("aiapprop_rgAG.png", width=6.8, height=3.3, units="in", dpi=300)

a41 + a42 + a43 + a44 +
  plot_layout(ncol=2 )
ggsave("figurea4demo.pdf", width=14, height=7, units="in")
```


## Additional calculations

```{r}
# Discussed in text--using AI to help with exams and improve writing
prop.table(table(ai$skills_helpsexams))
prop.table(table(ai$skills_improvewriting))

# Discussed in text Discussed in text--growth of AI in academics
prop.table(table(ai$useofAIinclassroom))
```


```{r}
## Initial tests for significance
ct <- function(group, outcome){
 ctest <- fisher.test(table(group, outcome))
 pvalue <- ctest$p.value
 names(pvalue) <- "pvalue"
 means <- tapply(outcome, group, mean, na.rm=T)
 return(round(c(means, pvalue), digits=3))
}

knitr::kable(round(rbind(ct(ai$firstgen_binary, ai$usage_entertainment),
ct(ai$firstgen_binary, ai$usage_worktasks),
ct(ai$firstgen_binary, ai$usage_language),
ct(ai$firstgen_binary, ai$usage_recipes),
ct(ai$firstgen_binary, ai$usage_newideas),
ct(ai$firstgen_binary, ai$usage_coursework),
ct(ai$firstgen_binary, ai$usage_writingpapers),
ct(ai$firstgen_binary, ai$usage_assignments),
ct(ai$firstgen_binary, ai$usage_data),
ct(ai$firstgen_binary, ai$usage_music),
ct(ai$firstgen_binary, ai$usage_travel),
ct(ai$firstgen_binary, ai$usage_creativewriting)), digits=2))





knitr::kable(round(rbind(ct(ai$female, ai$usage_entertainment),
ct(ai$female, ai$usage_worktasks),
ct(ai$female, ai$usage_language),
ct(ai$female, ai$usage_recipes),
ct(ai$female, ai$usage_newideas),
ct(ai$female, ai$usage_coursework),
ct(ai$female, ai$usage_writingpapers),
ct(ai$female, ai$usage_assignments),
ct(ai$female, ai$usage_data),
ct(ai$female, ai$usage_music),
ct(ai$female, ai$usage_travel),
ct(ai$female, ai$usage_creativewriting)), digits=2))



knitr::kable(round(rbind(ct(ai$highgpa, ai$usage_entertainment),
ct(ai$highgpa, ai$usage_worktasks),
ct(ai$highgpa, ai$usage_language),
ct(ai$highgpa, ai$usage_recipes),
ct(ai$highgpa, ai$usage_newideas),
ct(ai$highgpa, ai$usage_coursework),
ct(ai$highgpa, ai$usage_writingpapers),
ct(ai$highgpa, ai$usage_assignments),
ct(ai$highgpa, ai$usage_data),
ct(ai$highgpa, ai$usage_music),
ct(ai$highgpa, ai$usage_travel),
ct(ai$highgpa, ai$usage_creativewriting)), digits=2))



knitr::kable(round(rbind(ct(ai$racegroupsmall, ai$usage_entertainment),
ct(ai$racegroupsmall, ai$usage_worktasks),
ct(ai$racegroupsmall, ai$usage_language),
ct(ai$racegroupsmall, ai$usage_recipes),
ct(ai$racegroupsmall, ai$usage_newideas),
ct(ai$racegroupsmall, ai$usage_coursework),
ct(ai$racegroupsmall, ai$usage_writingpapers),
ct(ai$racegroupsmall, ai$usage_assignments),
ct(ai$racegroupsmall, ai$usage_data),
ct(ai$racegroupsmall, ai$usage_music),
ct(ai$racegroupsmall, ai$usage_travel),
ct(ai$racegroupsmall, ai$usage_creativewriting)), digits=2))
```




